# 导入Flask类库
from flask import Flask, request
import flask
import os
from ruamel import yaml
from testcases.pinggu.huli_qiye_pinggu_test_test import TestCaseHuliQiyePingguTest
from testcases.pinggu.huli_zhuanye_pinggu_test_test import TestCaseHuliZhuanyePingguTest
from testcases.pinggu.jibingfengxian_pinggu_test_test import TestCaseJibingfengxianPingguTest
from testcases.pinggu.laonian_yiyu_pinggu_test_test import TestCaseLaonianYiyuPingguTest
from testcases.pinggu.naozuzhong_pinggu_test_test import TestCaseNaozuzhongPingguTest
from testcases.pinggu.sas_jiaolu_pinggu_test_test import TestCaseSasJiaoluPingguTest
from testcases.pinggu.sds_yiyuzheng_pinggu_test_test import TestCaseSdsYiyuzhengPingguTest
from testcases.pinggu.shenjingxinli_pinggu_test_test import TestCaseShenjingxinliPingguTest
from testcases.pinggu.xinjigengsi_pinggu_test_test import TestCaseXinjigengsiPingguTest
from testcases.pinggu.zhongyiyao_pinggu_test_test import TestCaseZhongyiyaoPingguTest

app = Flask(__name__)


@app.route('/pinggu', methods=['post'])
def request_url():
    id = flask.request.values.get('id_card')
    pinggu_type = flask.request.values.get('pinggu_type')
    change_yaml('id_card', id)
    if pinggu_type == 'huliqiye':
        TestCaseHuliQiyePingguTest().test_start()
    elif pinggu_type == 'hulizhuanye':
        TestCaseHuliZhuanyePingguTest().test_start()
    elif pinggu_type == 'jibingfengxian':
        TestCaseJibingfengxianPingguTest().test_start()
    elif pinggu_type == 'laonianyiyu':
        TestCaseLaonianYiyuPingguTest().test_start()
    elif pinggu_type == 'naocuzhong':
        TestCaseNaozuzhongPingguTest().test_start()
    elif pinggu_type == 'sasjiaolv':
        TestCaseSasJiaoluPingguTest().test_start()
    elif pinggu_type == 'sdsyiyuzheng':
        TestCaseSdsYiyuzhengPingguTest().test_start()
    elif pinggu_type == 'shenjingxinli':
        TestCaseShenjingxinliPingguTest().test_start()
    elif pinggu_type == 'xinjigengsi':
        TestCaseXinjigengsiPingguTest().test_start()
    elif pinggu_type == 'zhongyiyao':
        TestCaseZhongyiyaoPingguTest().test_start()
    else:
        return '字段传错啦，目前支持字段列表{"护理企业":huliqiye,"护理专业":hulizhuanye,"疾病防治":jibingfengxian,"老年抑郁":laonianyiyu,"脑卒中":naocuzhong,"SAS焦虑":sasjiaolv,"SDS抑郁症":sdsyiyuzheng,"神经心理评估":shenjingxinli,"心肌梗死评估":xinjigengsi,"中医药评估":zhongyiyao}'

    return 'done'


def change_yaml(key, value):
    '''
    只支持改编数字类型
    :param key:
    :param value:
    :return:
    '''
    # 获取当前脚本所在文件夹路径
    curPath = os.path.dirname(os.path.realpath(__file__))
    # 获取当前文件的上一级路径
    last = os.path.dirname(curPath)
    # 获取yaml文件路径
    yamlPath = os.path.join(last, "env.yaml")
    with open(yamlPath, encoding="utf-8") as f:
        content = yaml.load(f, Loader=yaml.RoundTripLoader)
        # 修改yml文件中的参数
        content[key] = int(value)
    with open(yamlPath, 'w', encoding="utf-8") as nf:
        yaml.dump(content, nf, Dumper=yaml.RoundTripDumper)


# 启动实施（只在当前模块运行）
if __name__ == '__main__':
    app.run(host="0.0.0.0")
